package com.qianfeng.xiaxk.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.alibaba.fastjson.JSON;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.lidroid.xutils.view.annotation.ContentView;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.qianfeng.xiaxk.R;
import com.qianfeng.xiaxk.bean.UserInfo;
import com.qianfeng.xiaxk.fragment.ChatFragment;
import com.qianfeng.xiaxk.fragment.DestinationFragment;
import com.qianfeng.xiaxk.fragment.DiscoveryFragment;
import com.qianfeng.xiaxk.fragment.MineFragment;
import com.qianfeng.xiaxk.fragment.RecommendFragment;
import com.qianfeng.xiaxk.inter.OnFragmentChangeListener;
import com.qianfeng.xiaxk.utils.FragmentTagUtils;
import com.qianfeng.xiaxk.utils.LoginUtils;

import java.util.ArrayList;

import javax.xml.transform.Result;

@ContentView(R.layout.activity_main)
public class MainActivity extends AppCompatActivity {

    @ViewInject(R.id.main_footer_rgs)
    public RadioGroup footGroups;
    @ViewInject(R.id.main_footer_chat_rb)
    private RadioButton chatRadioButton;
    private ArrayList<Fragment> fragments = new ArrayList<>();
    FragmentTagUtils utils;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ViewUtils.inject(this);
        initView();
    }

    private void initView() {
        fragments.add(RecommendFragment.newInstance(null, null));
        fragments.add(DestinationFragment.newInstance(null, null));
        fragments.add(ChatFragment.newInstance(null, null));
        fragments.add(DiscoveryFragment.newInstance(null, null));
        fragments.add(MineFragment.newInstance(null, null));  //个人中心
        utils = new FragmentTagUtils(this, footGroups, R.id.fragment_main_container, getSupportFragmentManager(), fragments);

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        Log.e("", "----mainActivity-onActivityResult");

        if (requestCode == 1 && resultCode == Activity.RESULT_OK) {
            final String id = data.getStringExtra("id");
            final String name = data.getStringExtra("name");
            final String url = "http://api2.xiaxk.com/api/user?username=" + id;


            new HttpUtils().send(HttpRequest.HttpMethod.GET, url, new RequestCallBack<String>() {
                @Override
                public void onSuccess(ResponseInfo<String> responseInfo) {
                    UserInfo userInfo = JSON.parseObject(responseInfo.result, UserInfo.class);
                    String vid = userInfo.getVid();
                    String icon = userInfo.getHeadphoto();
                    String state = userInfo.getState();

                    LoginUtils.login(MainActivity.this, name, id, icon, vid, state, userInfo.getSex());
                    Log.e("", "-----异步查询信息结束了");
                    chatRadioButton.setChecked(true);////聊天的那个RadioButton选中
                }

                @Override
                public void onFailure(HttpException e, String s) {
                    Log.e("","--失败了"+s);
                }
            });
            //LoginUtils.login(this,name,id,icon);////将用户的信息存起来，按理是要发生到服务器端的，这里存在sharedPreference

        }
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
    }
}
